package com.liangtian.mdm.system.forums.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.liangtian.mdm.base.controller.BaseController;
import com.liangtian.mdm.base.utils.ResultObject;
import com.liangtian.mdm.base.utils.ShiroSessionUtils;
import com.liangtian.mdm.dm.common.pojo.ResponseMsg;
import com.liangtian.mdm.util.RequestUtils;
import com.liangtian.mdm.system.forums.model.SysForums;
import com.liangtian.mdm.system.forums.service.SysForumsService;
import com.liangtian.mdm.system.user.model.SysUser;
import com.liangtian.mdm.util.ConstantBean;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sysForums")
public class SysForumsController extends BaseController {

    @Autowired
    SysForumsService sysForumsServiceImp;
    // Session中保存的当前登录用户的信息
    //@Value("${ConstantBean.CURRENT_SESSION_USER}")
    //private String ConstantBean.CURRENT_SESSION_USER;

    /**
     * <p>TODO  栏目树查询</p>
     * <p>@param request
     * <p>@return </p>
     *
     * @author : xiaoliu
     * @date :2017年5月4日下午3:41:13
     */
    @RequestMapping(value = "/querySysForumsTree", method = RequestMethod.POST)
    @ResponseBody
    public String querySysForumsTree(HttpServletRequest request) {
        //查询条件
        Map<String, Object> param = RequestUtils.paramToMap(request);
        System.out.println(param);
        System.out.println("=========querySysForumsTree==========");
        List<SysForums> children = sysForumsServiceImp.querySysForumsTree(param);

        return JSON.toJSONString(children);
    }

    /**
     * <p>TODO  栏目数据查询，分页及筛选条件</p>
     * <p>@param request
     * <p>@return </p>
     *
     * @author : xiaoliu
     * @date :2017年5月4日下午3:41:13
     */
    @ResponseBody
    @RequestMapping(value = "/querySysForumsList", method = RequestMethod.GET)
    public JSONObject querySysForumsList(HttpServletRequest request, SysForums sysForums) {
        Map paramMap = RequestUtils.paramToMap(request, SysForums.class, false);
        PageInfo pageInfo = sysForumsServiceImp.querySysForumsByContion(paramMap);
        List l = pageInfo.getList();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("rows", l);// JSONArray
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;
    }

    /**
     * <p>TODO 新增</p>
     * <p>@param model
     * <p>@param request
     * <p>@return </p>
     *
     * @author : xiaoliu
     * @date :2017年5月4日下午4:53:00
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public ResultObject insert(SysForums sysForums, HttpServletRequest request) {
        // 获取session中用户
        SysUser user = ShiroSessionUtils.getSessionSysUser(ConstantBean.CURRENT_SESSION_USER);
        String userId = user.getUserId() + "";
        sysForums.setCreator(userId.toString());
        sysForums.setState(0);
        sysForums.setStutes(0);
        System.out.println(sysForums);
        int count = sysForumsServiceImp.insertSysForums(sysForums);
        ResultObject resultObject = new ResultObject(count, "新增成功", "新增失败");
        return resultObject;
    }

    @ResponseBody
    @RequestMapping(value = "/getForumsById", method = RequestMethod.POST)
    public JSONObject getForumsById(SysForums sysForums) {
        JSONObject jsonObject = new JSONObject();
        SysForums m = null;
        String forumId = sysForums.getForumId();
        try {
            m = sysForumsServiceImp.querySysForumsOne(forumId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        jsonObject.put("mo", m);
        return jsonObject;
    }

    /**
     * <p>TODO 栏目名称校验，是否已经存在</p>
     * <p>@param request
     * <p>@param sysForums
     * <p>@return </p>
     *
     * @author : xiaoliu
     * @date :2017年5月9日上午10:03:04
     */
    @ResponseBody
    @RequestMapping(value = "/checkForumsName", method = RequestMethod.POST)
    public JSONObject checkForumsName(HttpServletRequest request, SysForums sysForums) {
        JSONObject jsonObject = new JSONObject();
        ResponseMsg<String> rMsg = new ResponseMsg<String>();
        Map paramMap = RequestUtils.paramToMap(request, SysForums.class, false);
        try {
            int count = sysForumsServiceImp.checkForumsName(paramMap);
            rMsg.setRspCode(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        jsonObject.put("rmsg", rMsg);
        return jsonObject;
    }


    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JSONObject update(SysForums sysForums) {
        SysForums forum = sysForumsServiceImp.querySysForumsOne(sysForums.getForumId());
        //获取session中用户
        SysUser user = ShiroSessionUtils.getSessionSysUser(ConstantBean.CURRENT_SESSION_USER);
        ResponseMsg<String> rMsg = new ResponseMsg<String>();
        try {
            forum.setUpdator(user.getUserId().toString());
            forum.setForumName(sysForums.getForumName());
            forum.setDes(sysForums.getDes());
            forum.setIsSelect(sysForums.getIsSelect());
            forum.setParent(sysForums.getParent());
            sysForumsServiceImp.updateSysForums(forum);
            rMsg.setRspCode(0);
        } catch (Exception e) {
            e.printStackTrace();
            rMsg.setRspCode(-1);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("rMsg", rMsg);
        return jsonObject;
    }

    /**
     * 冻结，解冻
     */
    @ResponseBody
    @RequestMapping(value = "/updateSysForumsStatus", method = RequestMethod.POST)
    public ResultObject updateSysForumsStatus(SysForums sysForums) {
        ResultObject result = null;
        try {
            sysForumsServiceImp.updateSysForumsStatus(sysForums);
            result = new ResultObject(1, "操作成功", "操作失败");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultObject(2, "操作失败", "操作失败");
        }
        return result;
    }

    //提交数据

    @ResponseBody
    @RequestMapping(value = "/submitSysForumsData", method = RequestMethod.POST)
    public JSONObject submitSysForumsData(HttpServletRequest request, String[] ids) {
        ResponseMsg<String> rMsg = new ResponseMsg<String>();
        List<String> idsLst = Arrays.asList(ids);
        try {
            int count = sysForumsServiceImp.submitSysForumsData(idsLst);
            rMsg.setRspCode(count);
        } catch (Exception e) {
            e.printStackTrace();
            rMsg.setRspCode(-1);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("rMsg", rMsg);
        return jsonObject;
    }
    /**<p>TODO 查询指定文件夹下的图片，并封装结果集</p>
     * <p>@param request
     * <p>@return </p>
     * @author : xiaoliu
     * @date :2017年5月10日上午9:21:35
     */
//	@ResponseBody
//	@RequestMapping(value = "/getIocLst", method = RequestMethod.POST)
//	public JSONObject getIocLst(HttpServletRequest request) {
//		JSONObject jsonObject = new JSONObject();
//		
//		try {
//		
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	
//		return jsonObject;
//	}

    /**
     * 模型的物理删除功能
     *
     * @author : xiaoliu
     * @date :2016年9月29日上午9:20:29
     */
    @ResponseBody
    @RequestMapping(value = "/deleteForums", method = RequestMethod.GET)
    public JSONObject deleteForums(HttpServletRequest request, String[] ids) {// ids是前台复选框选择的List集合
        JSONObject jsonObject = new JSONObject();
        try {
            sysForumsServiceImp.deleteForums(ids);
            jsonObject.put("msg", "success");
        } catch (Exception e) {
            jsonObject.put("msg", "false");
            e.printStackTrace();
        }
        return jsonObject;
    }

}
